<?php
namespace Kuoqi\Interpreter;
class Ifs{
	static protected $assign,$code;
	static public function execCode($code,$assign = array()){
		self::$assign = $assign;
		self::$code = $code;
		self::__initializate();
		$code = self::$code;
		self::$code = '';
		return 	$code;	
	}
	static public function __initializate(){
		$data = self::$code;
		preg_match_all(self::need(),$data,$arr);
		foreach($arr[0] as $k=>$v){
			$data = str_replace($v,'<?php if('.$arr[1][$k].'):?>',$data);
		}
		preg_match_all(self::needelseif(),$data,$arr);
		foreach($arr[0] as $k=>$v){
			$data = str_replace($v,'<?php elseif('.$arr[1][$k].'):?>',$data);
		}
		preg_match_all(self::needelse(),$data,$arr);
		foreach($arr[0] as $k=>$v){
			$data = str_replace($v,'<?php else:?>',$data);
		}
		$data=str_replace(self::needend(),self::replacend(),$data);
		self::$code = $data;
	}
	
	static private function need(){
		return '/\<if\s+condition\=[\'\"]([\&\%\w\d\,\[\]\'\"\_\<\>\=\+\-\*\/\$\!\(\)]+)[\'\"]\s*\>/';
	}
	
	static private function needelseif(){
		return '/\<elseif\s+condition\=[\'\"]([\&\%\w\d\,\[\]\'\"\_\<\>\=\+\-\*\/\$\!\(\)]+)[\'\"]\s*\/\>/';
	}
	
	static private function needelse(){
		return '/\<else\s*\/{0,1}\>/';
	}
	
	static private function needend(){
		return '</if>';
	}
	
	static private function replace($val){
		return '<?php if('.$val.'):?>';
	}
	
	static private function replacend(){
		return '<?php endif;?>';
	}
}